package kr.ac.konkuk.db.quadtree.client;

import java.io.Closeable;
import java.io.IOException;

import kr.ac.konkuk.db.quadtree.server.service.QuadTreeService;
import kr.ac.konkuk.db.quadtree.server.service.QuadTreeService.Iface;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

public class QuadTreeClient implements Closeable {
	private TTransport transport;
	private TProtocol protocol;
	
	private QuadTreeClient(TTransport transport, TProtocol protocol) {
		this.transport = transport;
		this.protocol = protocol;
	}
	
	public static QuadTreeClient connect(String host, int port) throws TTransportException, IOException {
		TSocket socket = new TSocket(host, port);
		socket.setTimeout(10 * 1000);
		TTransport transport = new TFramedTransport(socket);
		TProtocol protocol = new TBinaryProtocol(transport);
		transport.open();
		
		return new QuadTreeClient(transport, protocol);
	}
	
	public Iface getQuadTreeService() {
		return new QuadTreeService.Client(protocol);
	}
	
	public void close() {
		transport.close();
	}
}
